import java.util.Arrays;

public class Rotate2 {

    public static void main(String[] args) {
        Rotate2 rotate2 = new Rotate2();
        int[] nums = {1,2,3,4,5,6,7};
        rotate2.process(nums, false);
        System.out.println(Arrays.toString(nums));
    }

    public void rotate1(int[] nums, int k) {
        int n = nums.length;
        k = k % n;
        if (k < n / 2) {
            while (k > 0) {
                process(nums, true);
                k--;
            }
        } else {
            k = n - k;
            while (k > 0) {
                process(nums, false);
                k--;
            }
        }
    }

    public void process(int[] nums, boolean flag) {
        int temp;
        if (flag) {
            temp = nums[nums.length - 1];
            for (int i = nums.length - 1; i > 0; i--) {
                nums[i] = nums[i - 1];
            }
            nums[0] = temp;
        } else {
            temp = nums[0];
            for (int i = 0; i < nums.length - 1; i++) {
                nums[i] = nums[i + 1];
            }
            nums[nums.length - 1] = temp;
        }
    }

    public void rotate(int[] nums, int k) {
        int n = nums.length;
        k = k % n;
        int[] temp = new int[k];
        int index = 0;
        for (int i = n - k; i < n; i++) {
            temp[index++] = nums[i];
        }
        int l = n - k - 1;
        int r = n - 1;
        while (l >= 0) {
            nums[r--] = nums[l--];
        }
        for (int i = 0; i < k; i++) {
            nums[i] = temp[i];
        }
    }
}
